package org.hilo.boot.app.config;

import java.io.PrintStream;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiStyle;
import org.springframework.core.env.Environment;

/**
 * 
 * @author zollty
 * @since 2020年11月3日
 */
public class HiloBootBanner implements Banner {

    private static final String BANNER = "////////////////////////////////////////////////////////////////////\r\n" + 
            "//                          _ooOoo_                               //\r\n" + 
            "//                         o8888888o                              //\r\n" + 
            "//                         88\" . \"88                              //\r\n" + 
            "//                         (| ^_^ |)                              //\r\n" + 
            "//                         O\\  =  /O                              //\r\n" + 
            "//                      ____/`---'\\____                           //\r\n" + 
            "//                    .'  \\\\|     |//  `.                         //\r\n" + 
            "//                   /  \\\\|||  :  |||//  \\                        //\r\n" + 
            "//                  /  _||||| -:- |||||-  \\                       //\r\n" + 
            "//                  |   | \\\\\\  -  /// |   |                       //\r\n" + 
            "//                  | \\_|  ''\\---/''  |   |                       //\r\n" + 
            "//                  \\  .-\\__  `-`  ___/-. /                       //\r\n" + 
            "//                ___`. .'  /--.--\\  `. . ___                     //\r\n" + 
            "//             .\"\" '<  `.___\\_<|>_/___.'  >'\"\".                   //\r\n" + 
            "//            | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |                 //\r\n" + 
            "//            \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /                 //\r\n" + 
            "//      ========`-.____`-.___\\_____/___.-`____.-'========         //\r\n" + 
            "//                           `=---='                              //\r\n" + 
            "//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //\r\n" + 
            "//            佛祖保佑       永不宕机      永无BUG                 //\r\n" + 
            "////////////////////////////////////////////////////////////////////\r\n" + 
            "\r\n" + 
            " __    __   __   __        ______   \r\n" + 
            "|  |  |  | |  | |  |      /  __  \\  \r\n" + 
            "|  |__|  | |  | |  |     |  |  |  | \r\n" + 
            "|   __   | |  | |  |     |  |  |  | \r\n" + 
            "|  |  |  | |  | |  `----.|  `--'  | \r\n" + 
            "|__|  |__| |__| |_______| \\______/  \r\n";

    private static final int STRAP_LINE_SIZE = 52;

    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream printStream) {
        printStream.println();
        printStream.println(BANNER);
        StringBuilder sbu = new StringBuilder();
        sbu.append(" :: ").append(environment.getProperty("spring.application.name", "Hilo APP"));
        String appVersion = getApplicationVersion(sourceClass);
        if (appVersion != null) {
            sbu.append(" v" + appVersion);
        }
        sbu.append(" :: ");
        printStream.println(getAnsiString(sbu.toString(), getBootVersion()));
        printStream.println();
    }
    
    protected String getApplicationVersion(Class<?> sourceClass) {
        Package sourcePackage = (sourceClass != null) ? sourceClass.getPackage() : null;
        return (sourcePackage != null) ? sourcePackage.getImplementationVersion() : null;
    }

    protected String getBootVersion() {
        return SpringBootVersion.getVersion();
    }

    protected String getAnsiString(String first, String version) {
        version = (version != null) ? " (v" + version + ")" : "";
        StringBuilder padding = new StringBuilder();
        while (padding.length() < STRAP_LINE_SIZE - (version.length() + first.length())) {
            padding.append(" ");
        }
        padding.append("using Spring Boot");
        return AnsiOutput.toString(AnsiColor.GREEN, first, AnsiColor.DEFAULT, padding.toString(),
                AnsiStyle.FAINT, version);
    }

}